home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / libpcap-0.0 / grammar.y < prev    next >
Encoding:
Lex Description  |  1995-02-11  |  6.7 KB  |  269 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  *
  22.  */
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: grammar.y,v 1.39 94/06/14 20:09:25 leres Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30. #include <sys/socket.h>
  31.  
  32. #include <net/if.h>
  33. #include <net/bpf.h>
  34.  
  35. #include <netinet/in.h>
  36. #ifdef LINUX
  37. #include <linux/if_ether.h>
  38. #else
  39. #include <netinet/if_ether.h>
  40. #endif
  41.  
  42. #include <stdio.h>
  43. #include <pcap.h>
  44. #include <pcap-namedb.h>
  45.  
  46. #include "gencode.h"
  47.  
  48. #define QSET(q, p, d, a) (q).proto = (p),\
  49.              (q).dir = (d),\
  50.              (q).addr = (a)
  51.  
  52. int n_errors = 0;
  53.  
  54. static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
  55.  
  56. static void
  57. yyerror(char *msg)
  58. {
  59.     ++n_errors;
  60.     bpf_error(msg);
  61.     /* NOTREACHED */
  62. }
  63.  
  64. #ifndef YYBISON
  65. pcap_parse()
  66. {
  67.     return (yyparse());
  68. }
  69. #endif
  70.  
  71. %}
  72.  
  73. %union {
  74.     int i;
  75.     u_long h;
  76.     u_char *e;
  77.     char *s;
  78.     struct stmt *stmt;
  79.     struct arth *a;
  80.     struct {
  81.         struct qual q;
  82.         struct block *b;
  83.     } blk;
  84.     struct block *rblk;
  85. }
  86.  
  87. %type    <blk>    expr id nid pid term rterm qid
  88. %type    <blk>    head
  89. %type    <i>    pqual dqual aqual ndaqual
  90. %type    <a>    arth narth
  91. %type    <i>    byteop pname pnum relop irelop
  92. %type    <blk>    and or paren not null prog
  93. %type    <rblk>    other
  94.  
  95. %token  DST SRC HOST GATEWAY
  96. %token  NET PORT LESS GREATER PROTO BYTE
  97. %token  ARP RARP IP TCP UDP ICMP
  98. %token  DECNET LAT MOPRC MOPDL
  99. %token  TK_BROADCAST TK_MULTICAST
  100. %token  NUM INBOUND OUTBOUND
  101. %token  LINK
  102. %token    GEQ LEQ NEQ
  103. %token    ID EID HID
  104. %token    LSH RSH
  105. %token  LEN
  106.  
  107. %type    <s> ID
  108. %type    <e> EID
  109. %type    <h> HID
  110. %type    <i> NUM
  111.  
  112. %left OR AND
  113. %nonassoc  '!'
  114. %left '|'
  115. %left '&'
  116. %left LSH RSH
  117. %left '+' '-'
  118. %left '*' '/'
  119. %nonassoc UMINUS
  120. %%
  121. prog:      null expr
  122. {
  123.     finish_parse($2.b);
  124. }
  125.     | null
  126.     ;
  127. null:      /* null */        { $$.q = qerr; }
  128.     ;
  129. expr:      term
  130.     | expr and term        { gen_and($1.b, $3.b); $$ = $3; }
  131.     | expr and id        { gen_and($1.b, $3.b); $$ = $3; }
  132.     | expr or term        { gen_or($1.b, $3.b); $$ = $3; }
  133.     | expr or id        { gen_or($1.b, $3.b); $$ = $3; }
  134.     ;
  135. and:      AND            { $$ = $<blk>0; }
  136.     ;
  137. or:      OR            { $$ = $<blk>0; }
  138.     ;
  139. id:      nid
  140.     | pnum            { $$.b = gen_ncode((u_long)$1,
  141.                            $$.q = $<blk>0.q); }
  142.     | paren pid ')'        { $$ = $2; }
  143.     ;
  144. nid:      ID            { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
  145.     | HID            {
  146.                   /* Decide how to parse HID based on proto */
  147.                   $$.q = $<blk>0.q;
  148.                   switch ($$.q.proto) {
  149.                   case Q_DECNET:
  150.                     $$.b =
  151.                         gen_ncode(__pcap_atodn((char *)$1),
  152.                         $$.q);
  153.                     break;
  154.                   default:
  155.                     $$.b =
  156.                         gen_ncode(__pcap_atoin((char *)$1),
  157.                         $$.q);
  158.                     break;
  159.                   }
  160.                 }
  161.     | EID            { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
  162.     | not id        { gen_not($2.b); $$ = $2; }
  163.     ;
  164. not:      '!'            { $$ = $<blk>0; }
  165.     ;
  166. paren:      '('            { $$ = $<blk>0; }
  167.     ;
  168. pid:      nid
  169.     | qid and id        { gen_and($1.b, $3.b); $$ = $3; }
  170.     | qid or id        { gen_or($1.b, $3.b); $$ = $3; }
  171.     ;
  172. qid:      pnum            { $$.b = gen_ncode((u_long)$1,
  173.                            $$.q = $<blk>0.q); }
  174.     | pid
  175.     ;
  176. term:      rterm
  177.     | not term        { gen_not($2.b); $$ = $2; }
  178.     ;
  179. head:      pqual dqual aqual    { QSET($$.q, $1, $2, $3); }
  180.     | pqual dqual        { QSET($$.q, $1, $2, Q_DEFAULT); }
  181.     | pqual aqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  182.     | pqual PROTO        { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
  183.     | pqual ndaqual        { QSET($$.q, $1, Q_DEFAULT, $2); }
  184.     ;
  185. rterm:      head id        { $$ = $2; }
  186.     | paren expr ')'    { $$.b = $2.b; $$.q = $1.q; }
  187.     | pname            { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
  188.     | arth relop arth    { $$.b = gen_relation($2, $1, $3, 0);
  189.                   $$.q = qerr; }
  190.     | arth irelop arth    { $$.b = gen_relation($2, $1, $3, 1);
  191.                   $$.q = qerr; }
  192.     | other            { $$.b = $1; $$.q = qerr; }
  193.     ;
  194. /* protocol level qualifiers */
  195. pqual:      pname
  196.     |            { $$ = Q_DEFAULT; }
  197.     ;
  198. /* 'direction' qualifiers */
  199. dqual:      SRC            { $$ = Q_SRC; }
  200.     | DST            { $$ = Q_DST; }
  201.     | SRC OR DST        { $$ = Q_OR; }
  202.     | DST OR SRC        { $$ = Q_OR; }
  203.     | SRC AND DST        { $$ = Q_AND; }
  204.     | DST AND SRC        { $$ = Q_AND; }
  205.     ;
  206. /* address type qualifiers */
  207. aqual:      HOST            { $$ = Q_HOST; }
  208.     | NET            { $$ = Q_NET; }
  209.     | PORT            { $$ = Q_PORT; }
  210.     ;
  211. /* non-directional address type qualifiers */
  212. ndaqual:  GATEWAY        { $$ = Q_GATEWAY; }
  213.     ;
  214. pname:      LINK            { $$ = Q_LINK; }
  215.     | IP            { $$ = Q_IP; }
  216.     | ARP            { $$ = Q_ARP; }
  217.     | RARP            { $$ = Q_RARP; }
  218.     | TCP            { $$ = Q_TCP; }
  219.     | UDP            { $$ = Q_UDP; }
  220.     | ICMP            { $$ = Q_ICMP; }
  221.     | DECNET        { $$ = Q_DECNET; }
  222.     | LAT            { $$ = Q_LAT; }
  223.     | MOPDL            { $$ = Q_MOPDL; }
  224.     | MOPRC            { $$ = Q_MOPRC; }
  225.     ;
  226. other:      pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
  227.     | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
  228.     | LESS NUM        { $$ = gen_less($2); }
  229.     | GREATER NUM        { $$ = gen_greater($2); }
  230.     | BYTE NUM byteop NUM    { $$ = gen_byteop($3, $2, $4); }
  231.     | INBOUND        { $$ = gen_inbound(0); }
  232.     | OUTBOUND        { $$ = gen_inbound(1); }
  233.     ;
  234. relop:      '>'            { $$ = BPF_JGT; }
  235.     | GEQ            { $$ = BPF_JGE; }
  236.     | '='            { $$ = BPF_JEQ; }
  237.     ;
  238. irelop:      LEQ            { $$ = BPF_JGT; }
  239.     | '<'            { $$ = BPF_JGE; }
  240.     | NEQ            { $$ = BPF_JEQ; }
  241.     ;
  242. arth:      pnum            { $$ = gen_loadi($1); }
  243.     | narth
  244.     ;
  245. narth:      pname '[' arth ']'        { $$ = gen_load($1, $3, 1); }
  246.     | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
  247.     | arth '+' arth            { $$ = gen_arth(BPF_ADD, $1, $3); }
  248.     | arth '-' arth            { $$ = gen_arth(BPF_SUB, $1, $3); }
  249.     | arth '*' arth            { $$ = gen_arth(BPF_MUL, $1, $3); }
  250.     | arth '/' arth            { $$ = gen_arth(BPF_DIV, $1, $3); }
  251.     | arth '&' arth            { $$ = gen_arth(BPF_AND, $1, $3); }
  252.     | arth '|' arth            { $$ = gen_arth(BPF_OR, $1, $3); }
  253.     | arth LSH arth            { $$ = gen_arth(BPF_LSH, $1, $3); }
  254.     | arth RSH arth            { $$ = gen_arth(BPF_RSH, $1, $3); }
  255.     | '-' arth %prec UMINUS        { $$ = gen_neg($2); }
  256.     | paren narth ')'        { $$ = $2; }
  257.     | LEN                { $$ = gen_loadlen(); }
  258.     ;
  259. byteop:      '&'            { $$ = '&'; }
  260.     | '|'            { $$ = '|'; }
  261.     | '<'            { $$ = '<'; }
  262.     | '>'            { $$ = '>'; }
  263.     | '='            { $$ = '='; }
  264.     ;
  265. pnum:      NUM
  266.     | paren pnum ')'    { $$ = $2; }
  267.     ;
  268. %%
  269.